package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.ShopDao;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ShopDaoImpl implements ShopDao {
    @Override
    public int insert(Shop shop) {
        String sql = "insert into shop (shopId,shopName,shopProvince,shopCity,shopDescribe,shopPhone,shopImage,shopAddress,addTime,shopLatitude,shopLongitude,shopDistrict) values(null,?,?,?,?,?,?,?,?,?,?,?)";
        Object obj[] = {
                shop.getShopName(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDescribe(),
                shop.getShopPhone(),
                shop.getShopImage(),
                shop.getShopAddress(),
                new Date(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopDistrict()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int delete(int shopId) {
        String sql = "delete from shop where shopId = ?";
        Object obj[]={shopId};
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int update(Shop shop) {

        String sql = "update shop set shopName=?,shopProvince=?,shopCity=?,shopDescribe=?,shopPhone=?,shopImage=?,shopAddress=?,addTime=?,shopLatitude=?,shopLongitude=?,shopDistrict=? where shopId=?";
        Object obj[] = {
                shop.getShopName(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDescribe(),
                shop.getShopPhone(),
                shop.getShopImage(),
                shop.getShopAddress(),
                new Date(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopDistrict(),
                shop.getShopId()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public List<Shop> selectAll(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();
        String sql = "select * from shop where 1=1 ";
        List<Object> params = new ArrayList<>();
        String keyword = map.get("keyword");
        if(keyword!=null && !"".equals(keyword)){
            sql+=" and shopName like ?";
            params.add("%"+keyword+"%");
        }
        String phone = map.get("shopPhone");
        if(phone!=null && !"".equals(phone)){
            sql+=" and shopPhone like ?";
            params.add("%"+phone+"%");
        }
        String shopStatus = map.get("shopStatus");
        if(shopStatus!=null && !"".equals(shopStatus)){
            sql+=" and shopStatus = ?";
            params.add(shopStatus);
        }
        String startTime = map.get("startTime");
        if(startTime!=null && !"".equals(startTime)){
            sql+=" and addTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        String num = map.get("pageNum");
        if(num!=null && !"".equals(num)){
            sql+=" limit ?,?";
            int pageNum = Integer.parseInt(num);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                Shop shop = new Shop(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getDate(9),
                        res.getString(10),
                        res.getString(11),
                        res.getInt(12),
                        res.getInt(13),
                        res.getInt(14),
                        res.getInt(15),
                        res.getString(16),
                        res.getString(17),
                        res.getString(18),
                        res.getString(19),
                        res.getString(20),
                        res.getString(21),
                        res.getString(22),
                        res.getString(23),
                        res.getString(24)
                );
                list.add(shop);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int total = 0;
        List<Shop> list = new ArrayList<>();
        String sql = "select count(0) from shop where 1=1 ";
        List<Object> params = new ArrayList<>();
        String keyword = map.get("keyword");
        if(keyword!=null && !"".equals(keyword)){
            sql+=" and shopName like ?";
            params.add("%"+keyword+"%");
        }
        String phone = map.get("phone");
        if(phone!=null && !"".equals(phone)){
            sql+=" and shopPhone like ?";
            params.add("%"+phone+"%");
        }
        String shopStatus = map.get("shopStatus");
        if(shopStatus!=null && !"".equals(shopStatus)){
            sql+=" and shopStatus = ?";
            params.add(shopStatus);
        }
        String startTime = map.get("startTime");
        if(startTime!=null && !"".equals(startTime)){
            sql+=" and addTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                total = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return total;
    }
}
